package vip.xiaonuo.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.modular.facegrouporgrelate.entity.FacegroupOrgRelate;
import vip.xiaonuo.modular.facegrouporgrelate.service.FacegroupOrgRelateService;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.topic.SysOrgEventTopic;

import javax.annotation.Resource;

import static reactor.event.selector.Selectors.$;

@Service
@Slf4j
public class SysOrgDeleteListener implements Consumer<Event<SysOrg>>, InitializingBean {
    @Autowired
    private Reactor reactor;

    @Resource
    private FacegroupOrgRelateService relateService;

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(SysOrgEventTopic.ORG_DELETE), this);
    }

    @Override
    public void accept(Event<SysOrg> sysOrgEvent) {
        // 把当前机构相关联的分组关联删掉
        SysOrg org = sysOrgEvent.getData();
        relateService.remove(new LambdaQueryWrapper<FacegroupOrgRelate>().eq(FacegroupOrgRelate :: getOrgId , org.getId()));
    }
}
